//
//  RegisterViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/8.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "RegisterViewController.h"
#import "AccountLoginViewController.h"
#import <MBProgressHUD.h>
#import "VervifyPhone.h"
#import "LoginNet.h"
#import "NSString+SortNameUtils.h"
#import "HomeViewController.h"
#import "BLNavigationController.h"

@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UITextField *telField;
@property (weak, nonatomic) IBOutlet UITextField *vervifyField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet UITextField *commitPwdField;
@property (nonatomic, assign) NSUInteger time;
@property (weak, nonatomic) IBOutlet UIButton *storeBtn;
@property (weak, nonatomic) IBOutlet UIButton *sellBtn;
@property (weak, nonatomic) IBOutlet UIButton *customBtn;
@property (nonatomic, assign) NSInteger userClass;// 是不是店主 默认是店主
@property (nonatomic, strong) NSTimer *timer;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.title = @"注册";
    self.userClass = 1;
    self.topView.layer.cornerRadius = 5;
    [self.storeBtn setImage:[UIImage imageNamed:@"regist_selected"] forState:UIControlStateSelected];
    [self.sellBtn setImage:[UIImage imageNamed:@"regist_selected"] forState:UIControlStateSelected];
    [self.telField becomeFirstResponder];
    
    UITapGestureRecognizer *hideKeyBoardRecongnizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
    [self.view addGestureRecognizer:hideKeyBoardRecongnizer];
    self.registerBtn.layer.cornerRadius = 5;
    self.codeBtn.layer.cornerRadius = 5;
}

- (void)hideKeyBoard {
    [self.view endEditing:YES];
}


#pragma mark 获取验证码
- (IBAction)getVervifyCode:(id)sender {
    UIButton *button = sender;
    if(self.time > 0) {
        return;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    NSString *tel = self.telField.text;
    if(tel.length == 0) {
        hud.label.text = @"请输入手机号码！";
        [hud hideAnimated:YES afterDelay:3];
        return;
    }
    
    if(tel.length != 11 || ![VervifyPhone vervifyTellphoneNumber:tel]) {
        hud.label.text = @"手机号码格式不正确";
        [hud hideAnimated:YES afterDelay:3];
        return;
    }
   
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.label.text = @"拼命加载中。。。";

    [LoginNet getCodeWithTel:tel codeType:1 completer:^(id resultObject, NSError *error) {
        if(error) {
            hud.mode = MBProgressHUDModeText;
            [hud hideAnimated:YES afterDelay:3];
            hud.label.text = [error localizedDescription];
            return;
        }
        [hud hideAnimated:YES];
    }];
    
    
    button.userInteractionEnabled = NO;
    self.time = 120;
    
    
    [button setTitle:@"120s后重新获取" forState:UIControlStateNormal];
     __weak typeof (self) weakSelf = self;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        weakSelf.time--;
        if(weakSelf.time == 0) {
            [button setTitle:@"获取验证码" forState:UIControlStateNormal];
            button.userInteractionEnabled = YES;
            [weakSelf.timer invalidate];
        }else {
            [button setTitle:[NSString stringWithFormat:@"%zis后重新获取",weakSelf.time] forState:UIControlStateNormal];
        }
    }];
    
}

- (IBAction)selectedStore:(id)sender {
    if(self.storeBtn.isSelected) {
        return;
    }
    self.sellBtn.selected = NO;
    self.storeBtn.selected = YES;
    self.customBtn.selected = NO;
    self.userClass = 1;
    
}

- (IBAction)selectedSell:(id)sender {
    if(self.sellBtn.isSelected) {
        return;
    }
    self.sellBtn.selected = YES;
    self.storeBtn.selected = NO;
    self.customBtn.selected = NO;
    self.userClass = 2;
    
}

- (void)changeTitle:(UIButton *)button {


}
- (IBAction)selectCutstom:(UIButton *)sender {
    if(self.sellBtn.isSelected) {
        return;
    }
    self.sellBtn.selected = NO;
    self.storeBtn.selected = NO;
    self.customBtn.selected = YES;
    self.userClass = 3;
}

- (IBAction)lookpwd:(id)sender {
    UIButton *btn = sender;
    if(btn.isSelected) {
        btn.selected = NO;
        self.pwdField.secureTextEntry = YES;
    }else {
    self.pwdField.secureTextEntry = NO;
        btn.selected = YES;
    }
    
    
}
- (IBAction)lookCommitPwd:(id)sender {
    UIButton *btn = sender;
    if(btn.isSelected) {
        btn.selected = NO;
        self.commitPwdField.secureTextEntry = YES;
    }else {
        self.commitPwdField.secureTextEntry = NO;
        btn.selected = YES;
    }

    
}

#pragma mark 注册
- (IBAction)registerAccount:(id)sender {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    NSString *tel = self.telField.text;
    NSString *code = self.vervifyField.text;
    NSString *pwd = self.pwdField.text;
    NSString *commitPwd = self.commitPwdField.text;
    
    if(tel.length == 0) {
        hud.label.text = @"请输入手机号码！";
        [hud hideAnimated:YES afterDelay:3];
        return;
    }
    
    if(tel.length != 11 || ![VervifyPhone vervifyTellphoneNumber:tel]) {
        hud.label.text = @"手机号码格式不正确";
        [hud hideAnimated:YES afterDelay:3];
        return;
    }
    
    if(code.length == 0) {
        hud.label.text = @"请输入验证码";
        [hud hideAnimated:YES afterDelay:3];
        return;
    }
    
    if(pwd.length == 0){
        hud.label.text = @"请输入新密码";
        [hud hideAnimated:YES afterDelay:3];
        return;
        
    }
    if(commitPwd.length == 0){
        hud.label.text = @"请输入确认密码";
        [hud hideAnimated:YES afterDelay:3];
        return;
    }
    
    if(![commitPwd isEqualToString:pwd]) {
        hud.label.text = @"两次密码不一致";
        [hud hideAnimated:YES afterDelay:3];
        return;
    }
    hud.mode = MBProgressHUDModeIndeterminate;
    //密码加密
    NSString *pwdMd5 = [pwd md5String];
    NSDictionary *dic = @{@"passWord":pwdMd5, @"userTel":tel, @"code":code, @"userClass": [NSString stringWithFormat:@"%li", self.userClass]};
    [LoginNet postRegister:dic completer:^(id resultObject, NSError *error) {
        if(error) {
            hud.mode = MBProgressHUDModeText;
            [hud hideAnimated:YES afterDelay:3];
            hud.label.text = [error localizedDescription];
            return;
        }
        [hud hideAnimated:YES];
        [self registerSuccess];
    }];
}

- (void)registerSuccess {
    __weak typeof (self) weakSelf = self;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"恭喜您注册成功！" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"去登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        AccountLoginViewController *loginController = [[AccountLoginViewController alloc] init];
        [weakSelf.navigationController pushViewController:loginController animated:YES];
        
    }]];
//    [alert addAction:[UIAlertAction actionWithTitle:@"去首页" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        UIWindow *window = [UIApplication sharedApplication].keyWindow;
//        HomeViewController *controller = [[HomeViewController alloc] init];
//        window.rootViewController = [[BLNavigationController alloc] initWithRootViewController:controller];
//        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
//
//    }]];
    [self presentViewController:alert animated:YES completion:nil];
    

}


- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self.timer invalidate];
}

@end
